﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using CloudSoft.Extensions;

using ERPStore.Extensions;

namespace ERPStore.Exporter.Providers
{
	public class SitemapExport : ExportBase
	{
		private XDocument m_Document;

		public SitemapExport()
		{

		}

		public override void InitializeSettings(Models.Settings settings)
		{
			settings.FileName = "fr.catalog.sitemap.xml";
		}

		public override string Name
		{
			get { return "sitemap"; }
		}

		public override string LeadSourceId
		{
			get { return "69B5B210-0EE7-4F3D-8800-30F1D30B1489"; }
		}

		public override void CreateDocument()
		{
			m_Document = new XDocument(new XDeclaration("1.0", "UTF-8", null));
			XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
			var root = new XElement(ns + "urlset");
			m_Document.Add(root);

			root.Add(new XAttribute(XNamespace.Xmlns + "image", "http://www.google.com/schemas/sitemap-image/1.1"));
		}

		public override void AddElementToDocument(ERPStore.Models.Product product, ERPStore.Models.ProductStockInfo stockInfo)
		{
			var host = GlobalConfiguration.Configuration.Settings.DefaultUrl;

			if (product.SalePrice.Value == 0)
			{
				return;
			}

			var loc = string.Format("http://{0}/produit/{1}/{2}", host, product.EncodedCode(), product.Link);
			var priority = "0.5";
			if (product.HasStock)
			{
				priority = "0.8";
			}

			var siteMapUrl = new XElement(XNamespace.None + "url"
				, new XElement("loc", loc)
				, new XElement("lastmod", string.Format("{0:yyyy-MM-dd}", product.LastUpdate))
				, new XElement("changefreq", "monthly")
				, new XElement("priority", priority));

			XNamespace image = "http://www.google.com/schemas/sitemap-image/1.1";

			if (product.HasPicture)
			{
				var pictureLoc = string.Format("http://{0}{1}", host, string.Format(product.DefaultImage.Url, 0, 0));
				siteMapUrl.Add(new XElement(image + "image"
					, new XElement(image + "loc", pictureLoc)
					, new XElement(image + "title", product.PageTitle ?? product.Title)
					, new XElement(image + "caption", product.ShortDescription)
				));
			}

			siteMapUrl.RemoveAttributes();

			m_Document.Root.Add(siteMapUrl);
		}

		public override void SaveDocument()
		{
			var path = Configuration.ConfigurationSettings.AppSettings["exportPath"];
			var fileName = System.IO.Path.Combine(path, Settings.FileName);

			var sb = new System.Text.StringBuilder();
			// Bidouille pour supprimer le xmlns dans l'element url
			// j'ai pas d'autre solution pour l'instant
			using (var tw = new System.IO.StringWriter(sb))
			{
				m_Document.Save(tw, SaveOptions.DisableFormatting);
				sb.Replace("<url xmlns=\"\">", "<url>");
				sb.Replace("utf-16", "UTF-8");
				tw.Close();
			}

			using (var f = System.IO.File.CreateText(fileName))
			{
				f.Write(sb.ToString());
				f.Flush();
				f.Close();
			}
		}

		public override void Dispose()
		{
			m_Document = null;
		}
	}
}
